
import { RuleState, type FilterRuleOptions, type RuleGenerationResult, type RuleStyleConfig } from './types';
import { hexToRgb } from './types';

// 创建基础规则生成器工具
export function createBaseRule(
    options: FilterRuleOptions,
    config: {
        name: string;
        class: string | null;
        defaultColor: string;
        defaultBgColor: string;
        fontSize: number;
        icon?: string;
        iconShape?: string;
    }
): RuleGenerationResult {
    const { label, value, soundEnabled, color, bgColor, displayEnabled } = options;

    // 确定规则状态
    const state = displayEnabled ? RuleState.Enabled : RuleState.Disabled;

    // 如果规则被禁用，返回空结果
    if (state === RuleState.Disabled) {
        return {
            content: '',
            state: RuleState.Disabled,
            isValid: true
        };
    }

    const rgbColor = color ? hexToRgb(color) : hexToRgb(config.defaultColor);
    const rgbBgColor = bgColor ? hexToRgb(bgColor) : hexToRgb(config.defaultBgColor);

    let result = `Show # ${config.name}-${label}\n`;

    // 添加基础条件
    if (config.class) {
        result += `  Class "${config.class}"\n`;
    }
    if (value) {
        result += `  BaseType "${value}"\n`;
    }

    // 添加样式
    result += `  SetTextColor ${rgbColor}\n`;
    result += `  SetBackgroundColor ${rgbBgColor}\n`;
    result += `  SetBorderColor ${rgbColor}\n`;
    result += `  SetFontSize ${config.fontSize}\n`;

    if (config.icon && config.iconShape) {
        result += `  MinimapIcon 0 ${config.icon} ${config.iconShape}\n`;
        result += `  PlayEffect ${config.icon}\n`;
    }

    // 音效设置
    if (soundEnabled) {
        result += `  CustomAlertSound "音效\\${label}.mp3"\n`;
    } else {
        result += "  DisableDropSound True\n  DisableAlertSound True\n";
    }

    // 添加 Continue
    result += "  Continue\n";

    return {
        content: result,
        state,
        isValid: true,
        warnings: []
    };
}

// 创建规则样式配置
export function createStyleConfig(
    fontSize: number,
    textColor: string,
    borderColor: string = textColor,
    backgroundColor: string = '0 0 0 0',
    iconConfig?: { size?: number; color?: string; shape?: string }
): RuleStyleConfig {
    return {
        fontSize,
        textColor,
        borderColor,
        backgroundColor,
        ...(iconConfig && {
            iconSize: iconConfig.size,
            iconColor: iconConfig.color,
            iconShape: iconConfig.shape
        })
    };
}